package com.lovo.controller;

import com.google.gson.Gson;
import com.lovo.bean.UserRole;
import com.lovo.service.UserRoleService;
import com.lovo.service.serviceImpl.UserRoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserRoleServlet",urlPatterns = "/userRole.do")

public class UserRoleServlet extends HttpServlet {
    private UserRoleService urs =new UserRoleServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("html/text;charset=utf-8");
        String method = request.getParameter("method");
        if(method==null||method.equals("")){

        }else if(method.equals("getRoleByUserId")){
            getRoleByUserId(request,response);
        }
    }

    private void getRoleByUserId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("userId");
        int userId =0;
        if(id!=null){
            userId =Integer.parseInt(id);
        }
        //service里面的方法
        UserRole userRole = urs.selectByUserId(userId);
        String s = new Gson().toJson(userRole);
        response.getWriter().write(s);
    }
}
